/*
 * this srrvlet prepares chat messages to send back to the jsp
 */
package controllers.classes;

import classes.HibernateUtil;
import dao.ChatDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Chat;

/**
 *
 * @author Achie
 */
public class ChatReceiveController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        long lessonid = Long.parseLong(request.getParameter("lessonid"));
        HibernateUtil hu = new HibernateUtil();
        ChatDao cd = new ChatDao(hu.openSession());
        List<Chat> chatlist = cd.findAllByLesson(lessonid, "chat"); //get all the chat objects from a specific lesson (chat messages)
        hu.close(); //close connection

        //set neccessary attributes to send back to the page
        request.setAttribute("lessonid", lessonid);
        request.setAttribute("chatlist", chatlist); //set message list

        //redirect to the chat window page
        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/chat_window.jsp");
        dispatcher.forward(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
